<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Anggota extends CI_Controller {

		function __construct(){
			parent::__construct();
			$this->load->model('Anggota_model');
			$this->load->helper('all_helper');
			if(!$this->session->userdata('rule')== '1' && !$this->session->userdata('username') ){
			redirect('kategori');
		}
			
		
		}

		function index(){
			
			$data['title'] = 'Data Anggota';
			$data['main_view'] = 'anggota/anggota';
			$data['status'] = 'Data Anggota';
			$data['urutan'] = $this->uri->segment(3);
			$data['query'] = $this->Anggota_model->getAnggota('list',FALSE,FALSE,FALSE);
			$this->load->view('index',$data);
		
		
		}
		function cari(){
			$data['title'] = 'Cari Data Anggota';
			$data['main_view'] = 'anggota/anggota';
			$data['status'] = 'Cari Data Anggota';
			$data['urutan'] = $this->uri->segment(3);
			$cari = $this->input->post('search');
			$data['query'] =$this->Anggota_model->getAnggota('cari',FALSE,$cari,FALSE,FALSE);
			$this->load->view('index',$data);
		
		}
		
		function add(){
			$this->form_validation->set_rules('nim','nim','required');
			$this->form_validation->set_rules('nama','nama','required');
			$this->form_validation->set_rules('tgl_lahir','tanggal lahir','required');
			$this->form_validation->set_rules('telp','telpon','required');
				if ($this->form_validation->run()== FALSE){
					$data['title'] = 'Tambah Data Anggota';
					$data['status'] = 'Tambah Data Anggota';
					$data['main_view']= 'anggota/add';
					$this->load->view('index',$data);
				}
				else{
				$data = array('nim'	=> $this->input->post('nim'),
							'nama'	=> $this->input->post('nama'),
							'jenis_kelamin'	=> $this->input->post('kelamin'),
							'tgl_lahir'		=> $this->input->post('tgl_lahir'),
							'no_telp'		=> $this->input->post('telp'));
							
					$this->Anggota_model->addAnggota($data);
				redirect('anggota');
				
				}
			
		
		}

		function edit(){
			$this->form_validation->set_rules('nim','nim','required');
			$this->form_validation->set_rules('nama','nama','required');
			$this->form_validation->set_rules('tgl_lahir','tanggal lahir','required');
			$this->form_validation->set_rules('telp','telpon','required');
				if ($this->form_validation->run()== FALSE){
					$data['title'] = 'Tambah Data Anggota';
					$data['main_view']= 'anggota/edit';
					$data['status'] = 'Edit Data Anggota';
					$id_member = $this->uri->segment(3);
					$data['row'] =$this->Anggota_model->getAnggota('by_id',$id_member,FALSE,FALSE,FALSE);
					$this->load->view('index',$data);
				}
				else{
				$id=$this->uri->segment(3);
				$data = array('nim'	=> $this->input->post('nim'),
							'nama'	=> $this->input->post('nama'),
							'jenis_kelamin'	=> $this->input->post('kelamin'),
							'tgl_lahir'		=> $this->input->post('tgl_lahir'),
							'no_telp'		=> $this->input->post('telp'));
								
					$this->Anggota_model->editAnggota($id,$data);
				redirect('anggota');
			
			}
		}
		
		
		function delete(){
			$id = $this->uri->segment(3);
			$this->Anggota_model->deleteAnggota($id);
			redirect('anggota');
			
		
		}
		
		
		
}